/*本模块功能:我的问题(老师)
        编写者:朱光曜
        编写时间:2017.11.03*/
$(function() {
	var string1 = '';
	/*列表头部*/
	var box1 = new  Confirm;
	var box = new  Alert;
	var loading1 = new Loading();//监控全局ajax添加loading
	loading1.init();
    $(document).ajaxSend(function(){
		loading1.show();
	
		});
	$(document).ajaxStop(function(){
		loading1.hide();
	
		});
	document.addEventListener('DOMSubtreeModified',function(e){//去除空格
		$(".publicText").attr("maxlength","20");
		
		
		$.map($(".publicText"),function (opt) {//长度限制
			maxLengthLimit.init(opt);
		});
		$(".publicText").bind('input propertychange',function(){
			
		    var regy = /\s/g;
			if(regy.test($(this).val())){
				$(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
			}
		    });
	});
	function formatDate(now) {//处理时间戳
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var date = now.getDate();
		var hour = now.getHours();
		var minute = now.getMinutes();
		if(month<10){month="0"+month;}
		if(date<10){date="0"+date;}
		if(hour<10){hour="0"+hour;}
		if(minute<10){minute="0"+minute;}
		return year + "-" + month + "-" + date + "   " + hour + ":" + minute;
	}

	

	function tabelModel(data) {//列表内容
		var time = new Date(data.problemList[key].problenTime); 
		//console.log(data.problemList[key].problenTime)
		return '<tr class="list" problemId="' + data.problemList[key].problemId + '">' +
		'<td class="col-lg-7"><a href="javascript:void(0)">' + data.problemList[key].problemName + '</a></td>' +
		'<td class="col-lg-4"><span class="fl">' + data.problemList[key].courseName + '</span></td>' +
		'<td class="col-lg-2"><span class="fl">' + data.problemList[key].problemCount + '个回答</span></td>' +
		'<td class="col-lg-4"><span class="fr">' + formatDate(time) + '</span></td>' +
		'</tr>';

	}
	var nowData = 1;
	var index = {
		init: function() {
			this.bind();
		},
		bind: function() {
			var self = this;
			this.data_content = {}; //搜索集
			this.tab_content = {}; //列表集
			this.parameter = {
				"pageNow": 1,
				"pageSize": 10
			};

			this.postData(this.parameter, nowData);
			$(".funBox .searchBtn").off("click").click(function() { //搜索点击


				var keyW = $("#keyWord").val();
				if(verification.specialCode(keyW) !== true) {
					self.alert1("不能输入特殊字符(@/'\"#$%&^*)");
					return;
				}
				var nowData = 1;
				var parameter = {
					"problemName": keyW,
					
					"pageNow": 1,
					"pageSize": 10
				};
				var j=0;
				//遍历删除空元素
				for(i in parameter){
					if(j==0){j++;continue;}
					if(parameter[i]==null||parameter[i]==""){
						delete parameter[i];
					}
					if(!isNaN(parameter[i])){
						parameter[i]=Number(parameter[i]);
					}
				}

				self.postData(parameter, nowData,1);
			});

		},
		postData: function(par_data, nowData,hunt) { //获得搜索及列表数据
			var self = this;
			var parameter = par_data;

			$.ajax({
				type: "post",
				url: "getMyProblemListByPost",
				async: true,
				data: {
					"jsonStr": JSON.stringify(parameter)
				},
				success: function(data, status, xhr) {
					var new_data = JSON.parse(data);
                    if(new_data.result == "success"){
                        self.data_content = new_data;
                        self.tab_content = new_data;


                        self.tab_add();

                        // console.log(new_data.totalPage+"啊啊"+new_data.pageNow)
                        var sumpage = new_data.totalPage;
                        if(sumpage<1){sumpage=1;};
                        var nowpage = 1;
                        var pagenumber = 10;
                        var parame = parameter;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                if(!parameter) {
                                    parameter = {
                                        "pageNow": nowpage,
                                        "pageSize": 10
                                    };

                                } else {
                                    parameter.pageNow = nowpage;

                                }
                                $.ajax({
                                    type: "post",
                                    url: "getMyProblemListByPost",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        //获取院系
                                        var msgNew = JSON.parse(msg);

                                        self.data_content = msgNew;
                                        self.tab_content = msgNew;


                                        self.tab_add();

                                    },
                                    error: function(data, status, xhr) {
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });
					}else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
					}
					

				},
				error: function(data, status, xhr) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});
		},
		
		tab_add: function() { //列表追加

			var self = this;
			var data = self.tab_content;
			var table = string1;

			//data.selectList={};

			var i = 0;
			for(key in data.problemList) {
				i++;
			}

			if(i > 0) {

				for(key in data.problemList) {

					table += tabelModel(data);
				}

				$(".mainContent .tableBox").html(table);
				//设置列表宽度(最大为9)
				var numMax = 0;
				$(".tableBox tr:first td").each(function() {
					var str = $(this).attr("class");
					var num = str.lastIndexOf("col-lg-") + 7;
					var num1 = parseFloat(str.substring(num, num + 1));
					numMax += num1;
				});

				$(".tableBox tr:first td").each(function() {
					var str = $(this).attr("class");
					var num = str.lastIndexOf("col-lg-") + 7;
					var num1 = parseFloat(str.substring(num, num + 1));
			
					$(this).css("width", num1 / numMax * 100 + "%");
				});
			} else {
				var trImg = "<tr><td colspan='5'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
				$(".mainContent .tableBox").html(string1+ trImg);
			}
			
			/**/
			
			
			
			$(".tableBox tr.list td").off("click").click(function(){//点击跳转
				var json=JSON.stringify({
					"pageName":"studentQuestionsAndAnswers",
					"paramData":{
						"isMark":1,
				     
				        "problemId":parseInt($(this).parents("tr").attr("problemId"))
					}
				});
				//console.log($(this).parents("tr").html());
				window.location.href="skipPage?jsonStr="+json;
			});
		},
		alert1: function(content1) { //alert提示

			box.contenthead = "提示";
			box.content = content1;

			box.success = function() {
				box.hide();
			};
			

			box.init();
			box.show();
		}

	};
	index.init();

});